package base.juc;


// 上下文切换问题
public class SynchronizedDemo {

    // 加锁
    static  Object lock = new Object();
    static int  c=0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (lock){
                    c++;
                }

            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (lock){
                    c--;
                }
            }
        }, "t2");
        t1.start();
        t2.start();
        t1.join();;
        t2.join();
        System.out.println(c);
    }
}
